package com.iaz.tech.tools.mq;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iaz.tech.tools.message.IMessageAble;

import lombok.Data;

/**
 * @author IAZ
 * @version 2.1.0
 * @since 2024-05-25 14:32:47
 * @description 消息容器类
 */
@Data
public class AZIMessageContainer implements IMessageSectionAbility{
	/**
     * 日志
     */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(AZIMessageContainer.class);
	
	/* 消息队列, 直至所属神经元 */
	private Map<UUID, List<IMessageAble>> messages = new HashMap<UUID, List<IMessageAble>>();
	/**
	 * 当前Timestamp
	 */
	private long timestamp = System.currentTimeMillis();

	/* 30 分钟 */
	private final static long MAX_TIME_OUT_FOR_MESSAGE = 1000 * 60 * 30;
	
	/**
	 * 获得
	 */
	public List<IMessageAble> getMessages() {
		List<IMessageAble> result = new ArrayList<>();
		try {
			for(Map.Entry<UUID, List<IMessageAble>> entry : this.messages.entrySet() ) {
				if (entry == null) {
					continue;
				}
				if (entry.getValue() == null ) {
					continue;
				}
				if (entry.getValue().isEmpty()) {
					continue;
				}
				result.addAll(entry.getValue());
			}
		} catch(Exception e) {
			logger.error("异常信息：" + e.getMessage());
		}
		return result;
	}

	@Override
	public List<IMessageAble> getMessages(UUID neuronID) {
		List<IMessageAble> result = new ArrayList<>();
		try {
			if (neuronID == null) {
				throw new Exception("变量没有输入");
			}
			
			if (this.messages.containsKey(neuronID)) {
				result = this.messages.get(neuronID);
				//this.messages.remove(neuronID);
				List<IMessageAble> msg = this.messages.get(neuronID);
				msg.clear();
			}
			
		} catch(Exception e) {
			logger.error("异常信息：" + e.getMessage());
		}
		return result;
	}

	@Override
	public void pushMessage(UUID neuronID, IMessageAble newmessage) {
		try {
			if (neuronID == null) {
				throw new Exception("变量没有输入");
			}
			if (this.messages.containsKey(neuronID)) {
				List<IMessageAble> msg = this.messages.get(neuronID);
				msg.add(newmessage);
			}else {
				List<IMessageAble> msg = new ArrayList<IMessageAble>();
				msg.add(newmessage);
				this.messages.put(neuronID, msg);
			}			
		} catch(Exception e) {
			logger.error("异常信息：" + e.getMessage());
		} 
	}
	/**
	 * 获得消息生成的时间戳
	 */
	@Override
	public long toTimestamp() {
		return this.timestamp;
	}

	/**
	 * 扫描Message 队列的时间，将过期的消息移除
	 * @param sendNeuronId
	 */
	public void scanMessagesForTime(UUID sendNeuronId) {
		try {
			if (sendNeuronId == null) {
				throw new Exception("变量没有输入");
			}
			
			long time_current_time = System.currentTimeMillis();

			for (Map.Entry<UUID, List<IMessageAble>> entry : this.messages.entrySet()) {
				if (entry == null) {
					continue;
				}
				if (entry.getValue() == null) {
					continue;
				}
				if (entry.getValue().isEmpty()) {
					continue;
				}
				List<IMessageAble> msg = entry.getValue();
				List<IMessageAble> msg_remove = new ArrayList<>();
				for (int i = 0; i < msg.size(); i++) {
					IMessageAble message = msg.get(i);
					if (message == null) {
						continue;
					}
					if (!message.toUUID().equals(sendNeuronId)) {
						continue;
					}

					long timestamp_cont = message.toTimestamp();
					if ( (time_current_time - timestamp_cont) >= MAX_TIME_OUT_FOR_MESSAGE ) {
						msg_remove.add(message);	
					}
				}
				/* 删除过期消息 */
				for (IMessageAble message : msg_remove) {
					msg.remove(message);
				}
			}
		} catch(Exception e){
			logger.error("scanMessagesForTime error, sendNeuronId: {}", sendNeuronId, e);
		}
	}
	/**
	 * 停止消息队列
	 * @param sendNeuronId
	 */
	public void stopMessagesForTime(UUID sendNeuronId) {
		try {
			if (sendNeuronId == null) {
				throw new Exception("变量没有输入");
			}

			for (Map.Entry<UUID, List<IMessageAble>> entry : this.messages.entrySet()) {
				if (entry == null) {
					continue;
				}
				if (entry.getValue() == null) {
					continue;
				}
				if (entry.getValue().isEmpty()) {
					continue;
				}
				List<IMessageAble> msg = entry.getValue();
				List<IMessageAble> msg_remove = new ArrayList<>();
				for (int i = 0; i < msg.size(); i++) {
					IMessageAble message = msg.get(i);
					if (message == null) {
						continue;
					}
					if (!message.toUUID().equals(sendNeuronId)) {
						continue;
					}
					msg_remove.add(message);
				}
				/* 删除过期消息 */
				for (IMessageAble message : msg_remove) {
					msg.remove(message);
				}
			}
		} catch(Exception e){
			logger.error("scanMessagesForTime error, sendNeuronId: {}", sendNeuronId, e);
		}
	}
	
}
